package com.qf.config;

import com.qf.filter.JwtVerifyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;

@Configuration
@EnableWebSecurity(debug = true)
@EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled = true,jsr250Enabled = true)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity web) throws Exception {


        web.ignoring().antMatchers("/swagger-ui.html","/webjars/**",
                "/swagger-resources/**","/swagger-ui.html/**","/v2/**","/csrf");



    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/user/login").permitAll()
                .anyRequest().authenticated();
        http.csrf().disable()
                .addFilter(new JwtVerifyFilter(this.authenticationManager()));
        http.cors();
    }
    @Autowired
//    private DataSource dataSource;
    private UserDetailsService userDetailsService;
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//         auth.inMemoryAuthentication()
//             .withUser(User.withUsername("zhangsan").password("111").roles("ADMIN").build())
//             .withUser(User.withUsername("lisi").password("222").roles("USER").build())
//             .withUser(User.withUsername("wangwu").password("333").roles("").build())
//             .passwordEncoder(NoOpPasswordEncoder.getInstance());

        //从数据库查询用户、角色、权限信息
        //JdbcUserDetailsManager类是SpringSecurity提供的用于查询数据库中存储的用户信息的类
//        JdbcUserDetailsManager manager = new JdbcUserDetailsManager(dataSource);
        //配置使用Manager完成用户信息查询校验
        auth.userDetailsService(userDetailsService)
                .passwordEncoder(NoOpPasswordEncoder.getInstance());

    }
}